package com.ruoyi.ysg.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ysg.annotation.AddTeacherId;
import com.ruoyi.ysg.domain.*;
import com.ruoyi.ysg.mapper.YsgSignRecordMapper;
import com.ruoyi.ysg.utils.YsgUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ysg.mapper.YsgCourseMapper;
import com.ruoyi.ysg.service.IYsgCourseService;

/**
 * 课程管理Service业务层处理
 *
 * @author xsz
 * @date 2025-09-24
 */
@Service
public class YsgCourseServiceImpl implements IYsgCourseService
{
    @Autowired
    private YsgCourseMapper ysgCourseMapper;

    @Autowired
    private YsgSignRecordMapper ysgSignRecordMapper;

    /**
     * 查询课程管理
     *
     * @param id 课程管理主键
     * @return 课程管理
     */
    @Override
    public YsgCourse selectYsgCourseById(Long id)
    {
        return ysgCourseMapper.selectYsgCourseById(id);
    }

    /**
     * 查询课程管理列表
     *
     * @param ysgCourse 课程管理
     * @return 课程管理
     */
    @Override
    @AddTeacherId
    public List<YsgCourse> selectYsgCourseList(YsgCourse ysgCourse)
    {
        return ysgCourseMapper.selectYsgCourseList(ysgCourse);
    }

    /**
     * 新增课程管理
     *
     * @param ysgCourse 课程管理
     * @return 结果
     */
    @Override
    public int insertYsgCourse(YsgCourse ysgCourse)
    {
        ysgCourse.setCreateTime(DateUtils.getNowDate());
        return ysgCourseMapper.insertYsgCourse(ysgCourse);
    }

    /**
     * 修改课程管理
     *
     * @param ysgCourse 课程管理
     * @return 结果
     */
    @Override
    public int updateYsgCourse(YsgCourse ysgCourse)
    {
        ysgCourse.setUpdateTime(DateUtils.getNowDate());
        return ysgCourseMapper.updateYsgCourse(ysgCourse);
    }

    /**
     * 批量删除课程管理
     *
     * @param ids 需要删除的课程管理主键
     * @return 结果
     */
    @Override
    public int deleteYsgCourseByIds(Long[] ids)
    {
        return ysgCourseMapper.deleteYsgCourseByIds(ids);
    }

    /**
     * 删除课程管理信息
     *
     * @param id 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteYsgCourseById(Long id)
    {
        return ysgCourseMapper.deleteYsgCourseById(id);
    }

    @Override
    public List<YsgStudent> selectAssociatedStudentList(YsgCourseStudent ysgCourseStudent) {
        return ysgCourseMapper.selectAssociatedStudentList(ysgCourseStudent);
    }

    @Override
    public int AssociateStudent(YsgCourseStudent ysgCourseStudent) {
        String associated = ysgCourseStudent.getAssociated();
        if("associate".equals(associated)) {
            ysgCourseMapper.insertCourseStudent(ysgCourseStudent);
        }else if("unAssociate".equals(associated)) {
            ysgCourseMapper.deleteCourseStudent(ysgCourseStudent);
        }
        return 1;
    }

    @Override
    public List<Map> getCourseSchedule(YsgCourseSchedule ysgCourseSchedule) {
        Integer[] sortArray = {1,2,3,4,5,6};
        List<Map> list = new ArrayList<>(sortArray.length);
        List<YsgCourseSchedule> ysgCourseSchedules = ysgCourseMapper.selectCourseSchedule(ysgCourseSchedule);
        if(ObjectUtils.isEmpty(ysgCourseSchedules)) {
            return Collections.EMPTY_LIST;
        }
        //构建map
//        Map<String, Object> courseTime1 = new HashMap<>();
//        Map<String, Object> courseTime2 = new HashMap<>();
//        Map<String, Object> courseTime3 = new HashMap<>();
//        Map<String, Object> courseTime4 = new HashMap<>();
//        Map<String, Object> courseTime5 = new HashMap<>();
//        Map<String, Object> courseTime6 = new HashMap<>();
        Map<Integer, List<YsgCourseSchedule>> sortMap = ysgCourseSchedules.stream().collect(Collectors.groupingBy(YsgCourseSchedule::getSort));
        for (int i = 0;i < sortArray.length;i++) {
            Map<String, Object> map = new HashMap<>();
            List<YsgCourseSchedule> ysgCourseScheduleList = sortMap.get(sortArray[i]);
            if(ObjectUtils.isEmpty(ysgCourseScheduleList)){
                map.put("time", Constants.COURSE_TIME[i]);
                list.add(map);
                continue;
            }
            Map<String, YsgCourseSchedule> scheduleMap = ysgCourseScheduleList.stream().collect(Collectors.toMap(YsgCourseSchedule::getDay, x -> x));
            map.put("time", Constants.COURSE_TIME[i]);
            for (Map.Entry<String, YsgCourseSchedule> entry : scheduleMap.entrySet()) {
                String courseName = entry.getValue().getCourseName();
                String location = entry.getValue().getLocation();
                String teacherName = entry.getValue().getTeacherName();
                HashMap<Object, Object> map1 = new HashMap<>();
                map1.put("courseName",courseName);
                map1.put("location",location);
                map1.put("teacherName",teacherName);
                map.put(entry.getKey(),map1);
            }
            list.add(map);
        }

        return list;
    }

    @Override
    public void addCourseSchedule(String jsonStr) {
        List<YsgCourseSchedule> ysgCourseSchedules = JSONArray.parseArray(jsonStr, YsgCourseSchedule.class);
        for (YsgCourseSchedule ysgCourseSchedule : ysgCourseSchedules) {
            YsgCourse ysgCourse = ysgCourseMapper.selectYsgCourseById(ysgCourseSchedule.getCourseId());
            ysgCourseSchedule.setCourseName(ysgCourse.getName());
            ysgCourseSchedule.setTeacherId(ysgCourse.getTeacherId());
            ysgCourseSchedule.setTeacherName(ysgCourse.getTeacherName());
            ysgCourseSchedule.setState("0");
        }
        ysgCourseMapper.insertCourseSchedule(ysgCourseSchedules);
    }

    @Override
    public List<YsgCourseSchedule> getTodayCourses(YsgCourseSchedule vo) {
        vo.setTeacherId(YsgUtils.getTeacherId());
        List<YsgCourseSchedule> ysgCourseSchedules = ysgCourseMapper.selectCourseSchedule(vo);
        if(ysgCourseSchedules.isEmpty()){
            return Collections.emptyList();
        }
        List<Long> classIds = ysgCourseSchedules.stream().map(YsgCourseSchedule::getClassId).collect(Collectors.toList());
        List<Map<Long,Object>> studentCountMap =  ysgCourseMapper.selectStudentCount(classIds);
        Map<Object, Map<Long, Object>> mapMap = studentCountMap.stream().collect(Collectors.toMap(x ->Long.valueOf(x.get("id").toString()) , x -> x));
        for (YsgCourseSchedule ysgCourseSchedule : ysgCourseSchedules) {
            Map<Long, Object> studentCountMap1 = mapMap.get(ysgCourseSchedule.getCourseId());
            if(ObjectUtils.isNotEmpty(studentCountMap1)) {
                ysgCourseSchedule.setStudentCount((Long)studentCountMap1.get("studentCount"));
            }
        }
        return ysgCourseSchedules;
    }

    @Override
    public List<YsgSignRecord> getSignInRecords(YsgSignRecord ysgSignRecord) {
        List<YsgSignRecord> signRecords = ysgSignRecordMapper.getSignInRecords(ysgSignRecord);
        return signRecords;
    }

    @Override
    public void signIn(YsgSignRecord ysgSignRecord) {
        ysgSignRecord.setCreateTime(DateUtils.getNowDate());
        ysgSignRecord.setCreateBy(YsgUtils.getTeacherName());
        ysgSignRecord.setTeacherId(YsgUtils.getTeacherId());
        ysgSignRecordMapper.insert(ysgSignRecord);
    }

    @Override
    public void signIn(List<YsgSignRecord> signRecords) {
        for (YsgSignRecord signRecord : signRecords) {
            signRecord.setCreateTime(DateUtils.getNowDate());
            signRecord.setCreateBy(YsgUtils.getTeacherName());
            signRecord.setTeacherId(YsgUtils.getTeacherId());
        }
        ysgSignRecordMapper.batchInsert(signRecords);
    }

    @Override
    public void rate(YsgSignRecord ysgSignRecord) {
        ysgSignRecordMapper.update(ysgSignRecord);
    }

    @Override
    public YsgSignRecord getSignInRecordById(Long id) {
        return ysgSignRecordMapper.selectSignInRecordById(id);
    }
}
